package com.deer.wms.ware.task.model.pushErp.enter;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.base.system.model.BillOutBO;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.model.PurchaseOrderBO;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.enums.SoTypeResolveEnum;
import com.deer.wms.ware.task.model.out.OutDetail;
import com.deer.wms.ware.task.model.pushErp.PushErpBean;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: xu
 * @Date: 2022/06/17/10:02
 * @Description: 其他出库
 */
@Component
public class RestsEnterBean implements PushErpBean {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;

    @Override
    public Integer gainFileType() {
        return SoTypeResolveEnum.RESTS.getStatenum();
    }

    @Override
    public Map<String, String> getBeanAndPush(SoMaster soMaster, List<OutDetail> outDetails, BaseAssist enterAssist) {
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        orderToErpBO.setBillTypeId(soMaster.getSoType());
        orderToErpBO.setSourceBillTypeId(soMaster.getBillType()); //
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("OUT"));
        orderToErpBO.setBillDate(DateUtils.getNowDateString());
        if (ObjectUtil.isNotEmpty(soMaster.getDeptId())) {
                Map<String, Object> map = new HashMap<>();
                map.put("billTypeId", soMaster.getBillType());
                map.put("billIds", Arrays.asList(soMaster.getBeforeNum()));
               List<BillOutBO> billOutBOS = pushErpService.OutSourceBill(map);
            soMaster.setDeptId(Optional.ofNullable(billOutBOS).map(item -> item.listIterator().next().getDeptId()).orElse(845881));
        }


        orderToErpBO.setDeptId(ObjectUtil.isNotEmpty(soMaster.getDeptId()) ? soMaster.getDeptId() : 845881);

        orderToErpBO.setDeptId(107);

        orderToErpBO.setCategoryId(451235);
        orderToErpBO.setCategoryId2(ObjectUtil.isNotEmpty(enterAssist.getCategoryId2()) ? enterAssist.getCategoryId2() : 950902); //默认其他调拨
        orderToErpBO.setCategoryId3(ObjectUtil.isNotEmpty(enterAssist.getCategoryId3()) ? enterAssist.getCategoryId3() : 41720); //默认其他调拨
        List<OrderDetailBO> details = new ArrayList<>();
        outDetails.forEach(item -> {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setSourceBillId(item.getErpBillId());//源单据id
            orderDetailBO.setSourceBillDetailRowIndex(item.getBomSplit());//行号
            orderDetailBO.setQty(item.getDeliveryQuantity());//数量
            if (soMaster.getCategoryId() == 542814) {
                orderDetailBO.setMtoNo(item.getMtoNo());
            }
                orderDetailBO.setBatchNo(item.getBatchName()); //计划跟踪号}
                orderDetailBO.setStockId(item.getErpAreaId()); //调出仓库
                orderDetailBO.setStockId2(enterAssist.getStockId2()); //调入仓库
                details.add(orderDetailBO);
            });
        orderToErpBO.setDetails(details);
        return  pushErpService.pushIMBIll(orderToErpBO);
    }
}
